package org.com.curry.lee.utils.algorithm.algorithm.impl;

import com.baidu.aip.ocr.AipOcr;

import org.com.curry.lee.annotation.Strategy;
import org.com.curry.lee.constant.StrategyConstants;
import org.com.curry.lee.utils.algorithm.algorithm.IDrawAlgorithm;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

@Component("baiDuAiAlgorithm")
@Strategy(strategyMode = StrategyConstants.StrategyMode.BAIDUAI)
public class BaiDuAiAlgorithm implements IDrawAlgorithm {
//    @Resource
//    private OCRImgConfig ocrImgConfig;

    @Override
    public String messageDraw(String photoPath) {

    /**
     * 通过百度AI扫描获取手写识别文字
     * @throws Exception 测试是否成功
     */
        String APP_ID = "";
        String API_KEY= "ocrImgConfig.getApikey()" ;
        String SECRET_KEY = "ocrImgConfig.getSecretkey(" ;
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        System.out.println(APP_ID+"-"+API_KEY+"-"+SECRET_KEY);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true"); // 检测图片朝上
        options.put("detect_language", "true");  // 检测语言,默认是不检查
        options.put("probability", "true");   //是否返回识别结果中每一行的置信度
        // 参数为本地图片路径
        try {
            JSONObject res = client.handwriting(photoPath,options);
            //接口返回的全部内容
            System.out.println(res.toString());
            if (res != null) {
                JSONArray wordsResultesult = res.getJSONArray("words_result");
                JSONObject jsonObject = wordsResultesult.getJSONObject(0);
                String words = jsonObject.getString("words");
                return words;
            } else {
                return "";
            }
        }catch (JSONException e){
            e.printStackTrace();
        }
        return "";
    }
}
